package com.ld.oauth2.config.oauth;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFactory;

import javax.sql.DataSource;

@Configuration
@RequiredArgsConstructor
public class AuthTokenConfig {

//    @Autowired
//    private RedisConnectionFactory redisConnectionFactory;

    private final DynamicDataSourceProperties dynamicDataSourceProperties;

    @Autowired
    private CustomAccessTokenConvertor customAccessTokenConvertor;


    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.master")
    @Bean
    public DataSource dataSource() {
        return new DruidDataSource();
    }

    public static final String STRING_KEY = "ld-key";

    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        //指定签名的秘钥 现在是对称加密
        //converter.setSigningKey(STRING_KEY);
        //采用非对称加密
        //第一个参数是秘钥证书文件 第二个是密码 认证服务器是用私钥加密的
        KeyStoreKeyFactory factory = new KeyStoreKeyFactory(new ClassPathResource("ld-oauth2.jks"),"ld-oauth2".toCharArray());
        //getKeyPair是拿别名
        converter.setKeyPair(factory.getKeyPair("ld-oauth2"));
        converter.setAccessTokenConverter(customAccessTokenConvertor);
        return converter;
    }



}
